package com.petalsgroup.petals.service.user.impl;

import com.petalsgroup.petals.dao.Animation.AnimationDao;
import com.petalsgroup.petals.dao.Animation.impl.AnimationDaoImpl;
import com.petalsgroup.petals.dao.Comment.CommentDao;
import com.petalsgroup.petals.dao.Comment.Impl.CommentDaoImpl;
import com.petalsgroup.petals.dao.user.CommentsDao;
import com.petalsgroup.petals.dao.user.impl.CommentsDaoImpl;
import com.petalsgroup.petals.pojo.AnimationSimpleDO;
import com.petalsgroup.petals.pojo.CommentsDO;
import com.petalsgroup.petals.pojo.CommentsVO;
import com.petalsgroup.petals.service.user.CommentsService;

import java.util.ArrayList;
import java.util.List;

public class CommentsServiceImpl implements CommentsService {
    private CommentsDao commentsDao = new CommentsDaoImpl();
    private AnimationDao animationDao = new AnimationDaoImpl();
    private CommentDao commentDao = new CommentDaoImpl();
    @Override
    public List<CommentsVO> findCommentsByUid(Integer uid) {
        List<CommentsVO> list = new ArrayList<>();
        List<CommentsDO> commentsList = commentsDao.findCommentsByUid(uid);

        for (CommentsDO commentsDO:commentsList) {
            CommentsVO commentsVO = new CommentsVO();
            commentsVO.setCommentsDO(commentsDO);
            commentsVO.setAnimationSimpleDO(animationDao.getAnimationSimple(commentsDO.getAid()));
            list.add(commentsVO);
        }
        return list;
    }

    @Override//删除评论
    public void deleteCommentsByid(Integer comid) {
        //通过cid找到 aid
        Integer aid = commentsDao.findAidByCid(comid);
        //删除评论
        commentsDao.deleteCommentsByid(comid);
        //更新分数
        commentDao.updateAnimationScore(aid);
        //更新人数
        commentsDao.updateNum(aid);

    }

    @Override//搜索评论
    public List<CommentsVO> findCommentsByAname(String selectname) {
        //通过动漫名  模糊查询 aid
        List<CommentsVO> list = new ArrayList<>();
        List<AnimationSimpleDO> animationSimpleDOList= animationDao.findAnimationByAname(selectname);
        for (AnimationSimpleDO animationSimpleDO:animationSimpleDOList) {
            //System.out.println(animationSimpleDO);

            CommentsVO commentsVO = new CommentsVO();
            commentsVO.setAnimationSimpleDO(animationSimpleDO);
            commentsVO.setCommentsDO(commentsDao.findCommentsByAid(animationSimpleDO.getAid()));
            list.add(commentsVO);
        }
        return list;
    }
}
